/**
 * 
 */
package com.jbat.gwt.tiledmapcreator.client.views;

import com.jbat.gwt.tiledmapcreator.client.common.color.Color;
import com.jbat.gwt.tiledmapcreator.client.custom.TileMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.DragOverEvent;
import com.google.gwt.event.dom.client.DropEvent;
import com.google.gwt.event.dom.client.LoadEvent;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.touch.client.Point;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

/**
 */
public class EditMapViewImpl extends Composite implements EditMapView {

  interface EditMapViewImplUiBinder extends UiBinder<Widget, EditMapViewImpl> {
  }

  private static EditMapViewImplUiBinder uiBinder = GWT.create(EditMapViewImplUiBinder.class);

  private Presenter presenter;

  @UiField
  TileMap mapCanvas;

  public EditMapViewImpl() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  @Override
  public void drawImageAt(SafeUri imageString, final Point canvasLocation, final int width,
      final int height) {
    // convert the imageString to an ImageElement
    final Image img = new Image(imageString);
    mapCanvas.add(img);

    // add a load handler so the remaining code runs when img is loaded
    img.addLoadHandler(new LoadHandler() {
      @Override
      public void onLoad(LoadEvent event) {
        ImageElement imgElm = ImageElement.as(img.getElement());

        int xPos = (int) canvasLocation.getX();
        int yPos = (int) canvasLocation.getY();

        mapCanvas.drawImageAt(imgElm, xPos, yPos, width, height);
      }
    });
  }

  @Override
  public void drawLine(Color color, Point start, Point end) {
    mapCanvas.drawLine(color, start, end);
  }

  @Override
  public void drawRect(Color color, Point upperLeft, int width, int height) {
    mapCanvas.drawRect(color, upperLeft, width, height);
  }

  @Override
  public int getLeft() {
    return mapCanvas.getElement().getAbsoluteLeft();
  }

  @Override
  public int getTop() {
    return mapCanvas.getElement().getAbsoluteTop();
  }

  @Override
  public void setMapSize(int xUnits, int yUnits) {
    mapCanvas.setMapUnits(xUnits, yUnits);
  }

  @Override
  public void setPresenter(Presenter presenter) {
    this.presenter = presenter;
  }

  @UiHandler("mapCanvas")
  void handleClick(ClickEvent event) {
    // handle clicks
    int relativeX = event.getRelativeX(mapCanvas.getElement());
    int relativeY = event.getRelativeY(mapCanvas.getElement());
    presenter.select(new Point(relativeX, relativeY));
  }

  @UiHandler("mapCanvas")
  void handleDragOver(DragOverEvent event) {
    /*
     * event.preventDefault(); String text = event.getData("text/plain"); NativeEvent nativeEvent =
     * event.getNativeEvent(); int clientX = nativeEvent.getClientX(); int clientY =
     * nativeEvent.getClientY(); presenter.drawCell(text, new Point(clientX, clientY));
     */
  }

  @UiHandler("mapCanvas")
  void handleDrop(DropEvent event) {
    event.preventDefault();
    String text = event.getData("text/plain");
    NativeEvent nativeEvent = event.getNativeEvent();
    int clientX = nativeEvent.getClientX() - mapCanvas.getElement().getAbsoluteLeft();
    int clientY = nativeEvent.getClientY() - mapCanvas.getElement().getAbsoluteTop();
    presenter.drawCell(text, new Point(clientX, clientY));
  }
}
